package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.drg.center.entity.SysDepartPO;
import com.drg.center.mapper.SysDepartMapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class SysDepartDAO extends BaseDAO<SysDepartMapper, SysDepartPO> {

    public List<SysDepartPO> findSubList(String traces){
        LambdaQueryWrapper<SysDepartPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(SysDepartPO::getTraces, traces);
        lambdaQueryWrapper.orderByAsc(SysDepartPO::getTraces);
        return getBaseMapper().selectList(lambdaQueryWrapper);
    }

    public List<SysDepartPO> findList(Boolean onlyEnable){
        LambdaQueryWrapper<SysDepartPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(onlyEnable){
            lambdaQueryWrapper.eq(SysDepartPO::getDisabled, 0L);
        }
        lambdaQueryWrapper.orderByAsc(SysDepartPO::getDepartCode);
        return getBaseMapper().selectListIgnoreDeleted(lambdaQueryWrapper);
    }

    public List<SysDepartPO> findListByCodeList( Collection<?> codes){
        if(CollectionUtils.isEmpty(codes)){
            return Lists.newArrayList();
        }
        return findIn(SysDepartPO::getDepartCode, codes);
    }

    public List<String> findMainCodeList(){
        LambdaQueryWrapper<SysDepartPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDepartPO::getIsMain, true);
        List<SysDepartPO> poList = getBaseMapper().selectList(lambdaQueryWrapper);
        return poList.stream().map(SysDepartPO::getDepartCode).collect(Collectors.toList());
    }

}
